package com.nisco.dms.functional;

import static org.junit.Assert.*;

import org.junit.BeforeClass;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import com.nisco.dms.functional.BaseFunctionalTestCase;
import com.nisco.dms.test.util.Selenium2;
import com.nisco.dms.test.util.WebDriverFactory;

/**
 * 使用Selenium的功能测试基类.
 * 
 * 在BaseFunctionalTestCase的基础上，在整个测试期间仅启动一次Selenium.
 * 
 * @author robin
 */
public class BaseSeleniumTestCase extends BaseFunctionalTestCase {

	protected static Selenium2 s;

	@BeforeClass
	public static void init() throws Exception {
		createSeleniumOnce();
		loginAsUserIfNecessary();
	}

	/**
	 * 创建Selenium，仅创建一次.
	 */
	protected static void createSeleniumOnce() throws Exception {
		if (s == null) {
			//根据配置创建Selenium driver.
			String driverName = propertiesLoader.getProperty("selenium.driver");
			//chrome配置驱动
			System.setProperty("webdriver.chrome.driver","src/test/resources/webdriver/chromedriver.exe");
			//ie配置驱动
//			System.setProperty("webdriver.ie.driver","src/test/resources/webdriver/IEDriverServer.exe");
			WebDriver driver = WebDriverFactory.createDriver(driverName);

			s = new Selenium2(driver, baseUrl);
			s.setStopAtShutdown();
		}
	}

	/**
	 * 登录管理员, 如果用户还没有登录.
	 */
	protected static void loginAsUserIfNecessary() {
		s.open("/login");

		if ("nisco department manage system".equals(s.getTitle())) {
			s.type(By.name("username"), "admin");
			s.type(By.name("password"), "123456");
//			s.check(By.name("rememberMe"));
			s.click(By.id("submit_btn"));
			System.out.println("登录时的标题名:"+s.getTitle());
			assertEquals("nisco department manage system主菜单", s.getTitle());
		}
	}

}
